package com.xms.ribbonbutton.service;

import com.xms.core.exception.XmsException;
import com.xms.core.module.ModuleCollection;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.model.DependentDescriptor;
import com.xms.dependency.service.IDependencyBatchBuilder;
import com.xms.dependency.service.IDependencyDeleterService;
import com.xms.dependency.service.IDependencyUpdaterService;
import com.xms.ribbonbutton.core.RibbonButtonDefaults;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.utils.CollectionUtil;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.webresource.core.WebResourceDefaults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * RibbonButtonDependency
 * </p>
 *
 * @author migo
 * @since 2021/10/25
 */
@Service
public class RibbonButtonDependency implements IRibbonButtonDependency {
    private IRibbonButtonFinderService _ribbonButtonFinderService;
    private IDependencyBatchBuilder _dependencyBatchBuilder;
    private IDependencyUpdaterService _dependencyUpdaterService;
    private IDependencyDeleterService _dependencyDeleterService;

    @Autowired
    public void RibbonButtonDependency(IRibbonButtonFinderService ribbonButtonFinderService,
                                       IDependencyBatchBuilder dependencyBatchBuilder,
                                       IDependencyUpdaterService dependencyUpdaterService,
                                       IDependencyDeleterService dependencyDeleterService) {
        _ribbonButtonFinderService = ribbonButtonFinderService;
        _dependencyBatchBuilder = dependencyBatchBuilder;
        _dependencyUpdaterService = dependencyUpdaterService;
        _dependencyDeleterService = dependencyDeleterService;
    }

    @Override
    public boolean create(RibbonButton... entities) throws XmsException, SQLException {
        List<RibbonButton> buttons = Arrays.stream(entities).filter(x -> StringUtil.notEmpty(x.getJslibrary()) && x.getJslibrary().startsWith("$ws:")).collect(Collectors.toList());
        ;
        if (CollectionUtil.notEmpty(buttons)) {
            //依赖于web资源
            for (RibbonButton item : buttons) {
                _dependencyBatchBuilder.append(RibbonButtonDefaults.MODULE_NAME, item.getRibbonbuttonid(), WebResourceDefaults.MODULE_NAME, UUIDUtil.get(item.getJslibrary().replace("$ws:", "")));
            }
            _dependencyBatchBuilder.save();
        }
        return true;
    }

    @Override
    public boolean delete(UUID... id) throws XmsDependencyException, SQLException {
        return _dependencyDeleterService.deleteByDependentId(RibbonButtonDefaults.MODULE_NAME, id);
    }

    @Override
    public boolean update(RibbonButton entity) throws XmsException, SQLException {
        if (StringUtil.notEmpty(entity.getJslibrary()) && entity.getJslibrary().startsWith("$ws:")) {
            //依赖于web资源
            _dependencyUpdaterService.update(RibbonButtonDefaults.MODULE_NAME, entity.getRibbonbuttonid(), WebResourceDefaults.MODULE_NAME, UUIDUtil.get(entity.getJslibrary().replace("$ws:", "")));
        }

        return true;
    }

    @Override
    public DependentDescriptor getDependent(UUID dependentId) {
        RibbonButton result = _ribbonButtonFinderService.getById(dependentId);
        return result != null ? new DependentDescriptor() {
            {
                name = result.getLabel();
            }
        } : null;
    }

    @Override
    public int getComponentType() {
        return ModuleCollection.getIdentity(RibbonButtonDefaults.MODULE_NAME);
    }
}
